<?php
/**
 * Created by PhpStorm.
 * User: Rung-PC
 * Date: 2018/8/8
 * Time: 13:46
 */

namespace app\api\controller;
use app\home\controller\Common;

class Api extends Common{

    public function __construct(){
        parent::__construct();
        if(!request()->isAjax()){
            die('fuck you!');
        }
    }

    public function queryHomePage(){
          $homeType = input('homeType');
          $ary = db('banner')->where(['homeType'=>$homeType])->order('homeSort asc')->select();
            foreach($ary as $k=>$v){
                $ary[$k]['homeImg'] = '/static/banner/'.$v['homeImg'];
            }
            return $ary;
        }

    public function queryViewLeftMenu(){
            $menuId = input('menuId');
            $prefix = config('database.prefix');
            $ary = db('menu_html')->query('select * from '.$prefix.'menu where menu_parentid > 0 and menuId like "'.$menuId.'%"');
            return $ary;
        }

    public function queryParentMenuName(){
            $menuId = input('menuId');
            $prefix = config('database.prefix');
            $menuName = db('menu')->query('select menuName from '.$prefix.'menu where menu_parentid = 0 and menuId like "'.$menuId.'%"');
            return array('title'=>$menuName[0]['menuName']);
    }

    public function queryViewMenuNew(){
            $menuId = input('menuId');
            $menu = db('menu')->where(['menu_parentid'=>$menuId])->order('id asc')->select();
            foreach ($menu as $k=>$v){
                $children = db('menu')->where(['menu_parentid'=>$v['id']])->order('id asc')->select();
                $menu[$k]['children'] = $children;
            }
            return $menu;
    }

    public function getHtmlByMenuId(){
            $menuId = input('menuId');
            $ary = db('menu_html')->where(['menuId'=>$menuId])->find();
            return $ary;
        }

    public function checkUserName(){
            $user_name = input('userName');
            $result = db('user')->where(['user_name'=>$user_name])->find();
            $ary = ['status'=>0];
            if($result){
                $ary = ['status'=>1];
            }
            return $ary;
        }

    public function checkMail(){
            $mail = input('mail');
            $result = db('user')->where(['email'=>$mail])->find();
            $ary = ['status'=>0];
            if($result){
                $ary = ['status'=>1];
            }
            return $ary;
    }

    public function sendEmail(){
            $email = input('email');
            $code = $this->get_random2();
            session('code',$code);
            session('codeExpire',time());
            $body = '您正在申请注册,这是您的验证码:'.$code;
            $ary = ['code'=>1,'reason'=>'发送成功'];
            if(!$this->phpMail($email,'注册验证码',$body)){
                $ary = ['code'=>3001,'reason'=>'发送失败'];
            }
            return $ary;
    }

    public function commonRegist(){
            $username = input('username');
            $mail = input('mail');
            $commonRegJavaCode = input('commonRegJavaCode');
            $emailCode = input('emailCode');
            $password = md5(input('password'));

            $ary = ['code'=>1002,'reason'=>'注册失败!'];
            if(!session('?code') || ((time()-session('codeExpire')) > 60*30) || $emailCode != session('code')){
                $ary = ['code'=>1001,'reason'=>'邮箱验证码错误或已失效!'];
            }else {
                session(null);
                $data['user_name'] = $username;
                $data['email'] = $mail;
                $data['password'] = $password;
                $data['status'] = 1;
                $userId = db('user')->insertGetId($data);
                session('userId', $userId);
                session('userName', $username);
                session('userExpire', time());
                $ary = ['code'=>1000,'reason'=>'注册成功!'];
            }

            return $ary;
    }

    public function commonCode(){
            $code = input('code');
            $index = input('index');
            $ary = ['status'=>1];
            if( !captcha_check($code,$index))
            {
                $ary = ['status'=>0];
            }
            return $ary;

    }

    public function savePer(){
       $data = input();
       $result = db('user')
                 ->where(['id'=>session('userId')])
                 ->update($data);
       $ary = ['status'=>0];
       if($result){
           $ary = ['status'=>1];
       }
       return $ary;
    }
}